package com.qf.controller;


import com.qf.bean.AjaxMessage;
import com.qf.entity.User;
import com.qf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2020-09-03
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
//    @Autowired
    Jedis jedis;

    @RequestMapping("/exit")
    public AjaxMessage exit(String token){
        jedis.del(token);
        return new AjaxMessage(0,"已注销");
    }

    @RequestMapping("/register")
    public AjaxMessage register(User user) {
        user.setName("小贝帆");
        boolean b = userService.register(user);
        if (b){
            return new AjaxMessage(0,"注册成功");
        }
        return new AjaxMessage(1,"注册失败");
    }

    @RequestMapping("/login")
    public AjaxMessage login(User user) {
        boolean b = userService.login(user);
        if (b) {
            String token = UUID.randomUUID().toString();
            jedis.set(token,user.getId().toString());
            return new AjaxMessage(0,token);
        }
        return new AjaxMessage(1,"登陆失败");
    }

    @RequestMapping("/get")
    public AjaxMessage get(String token) {
        Integer id = Integer.valueOf(jedis.get(token));
        User user = userService.get(id);
        if (user != null ) {
            List list = new ArrayList();
            list.add(user);
            return new AjaxMessage(0,"",list);
        }
        return new AjaxMessage(1,"用户不存在，请登录！");

    }

    @RequestMapping("/update")
    public AjaxMessage update(String token) {
        Integer id = Integer.valueOf(jedis.get(token));
        User user = userService.get(id);
        if (user != null) {
            userService.update(user);
            return new AjaxMessage(0,"修改成功!");
        }

        return new AjaxMessage(1,"用户不存在!");
    }

    @RequestMapping("/checkPhone")
    public AjaxMessage checkPhone(String phone) {
        boolean b = userService.checkPhone(phone);
        if (b) {
            return new AjaxMessage(0,"");
        }
        return new AjaxMessage(1,"手机号已被注册");

    }

    /**
     * 获取验证码
     * @param phone
     * @return
     * @throws Exception
     */
    @RequestMapping("/sendCode")
    public String sendCode(String phone) throws Exception{

        String message = userService.sendCode(phone);

        return message;

    }

    /**
     * 验证码登陆
     */
    @RequestMapping("getCode")
    public AjaxMessage getCode(String phone,String inputCode) {
        Integer id = userService.getCode(phone, inputCode);
        if (id == 0) {
            return new AjaxMessage(1,"验证码错误");
        }
        String token = UUID.randomUUID().toString();
        jedis.set(token,id.toString());
        return new AjaxMessage(0,token);
    }

}
